package ks.client.gui.login;


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.SystemColor;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ks.client.gui.login.listeners.ConnectListener;
import ks.client.lobby.Lobby;

import ks.client.UserContext;

public class LoginGUI extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JPanel innerPanel = null;
	private JLabel hostLabel = null;
	private JLabel portLabel = null;
	private JLabel userLabel = null;
	private JLabel passLabel = null;
	private JTextField hostField = null;
	private JTextField portField = null;
	private JTextField userField = null;
	private JPasswordField passField = null;
	private JCheckBox selfRegCheck = null;
	private JButton connectButton = null;
	
	Lobby lobby;
	UserContext context;
	
	public LoginGUI(Lobby lobby) {
		super();
		
		this.lobby = lobby;
		this.context = lobby.getUserContext();
		
		initialize();
	}
	
	private void initialize() {
		this.setLayout(new FlowLayout());
		
		// Panel to be added.
		innerPanel = new JPanel();
		innerPanel.setPreferredSize(new Dimension(250, 100));
		innerPanel.setLayout(new GridLayout(5,2));
		
		hostLabel = new JLabel();
		hostLabel.setName("hostLabel");
		hostLabel.setText("Host:");
		
		hostField = new JTextField();
		hostField.setName("hostField");
		hostField.setText(context.getHost());
		
		portLabel = new JLabel();
		portLabel.setName("portLabel");
		portLabel.setText("Port:");
		
		portField = new JTextField();
		portField.setName("portField");
		portField.setText(""+context.getPort());
		
		userLabel = new JLabel();
		userLabel.setName("userLabel");
		userLabel.setText("User:");
		
		userField = new JTextField();
		userField.setName("userField");
		userField.setText(context.getUser());

		passLabel = new JLabel();
		passLabel.setText("Password:");
		passLabel.setName("passwordLabel");
		
		passField = new JPasswordField();
		passField.setName("passField");
		passField.setText(context.getPassword());
		
		selfRegCheck = new JCheckBox();
		selfRegCheck.setText("Create Account");
		
		connectButton = new JButton();
		connectButton.setText("Connect");
		connectButton.addActionListener(new ConnectListener(lobby));
		
		this.setLayout(new FlowLayout());
		this.setBackground(SystemColor.control);
		
		innerPanel.add(hostLabel);
		innerPanel.add(hostField);
		innerPanel.add(portLabel);
		innerPanel.add(portField);
		innerPanel.add(userLabel);
		innerPanel.add(userField);
		innerPanel.add(passLabel);
		innerPanel.add(passField);
		innerPanel.add(selfRegCheck);
		innerPanel.add(connectButton);
		
		this.add(innerPanel);
		
		this.invalidate();
		this.revalidate();
	}
	
	public String getHostname () {
		return this.hostField.getText();
	}
	
	public int getPort () {
		return Integer.parseInt(this.portField.getText());
	}
	
	public String getUsername () {
		return this.userField.getText();
	}
	
	public char[] getPassword () {
		return this.passField.getPassword();
	}
	
	public boolean getSelfRegister () {
		return this.selfRegCheck.isSelected();
	}
	
	public void setHostname (String hostname) {
		this.hostField.setText(hostname);
	}
	
	public void setPort (int port) {
		this.portField.setText(String.valueOf(port));
	}
	
	public void setUsername (String username) {
		this.userField.setText(username);
	}
	
	public void setPassword (String password) {
		this.passField.setText(password);
	}
	
	public void setSelfRegister (boolean selfReg) {
		this.selfRegCheck.setSelected(selfReg);
	}
	
	public void setFieldsEnabled (boolean enabled) {
		this.hostField.setEnabled(enabled);
		this.portField.setEnabled(enabled);
		this.userField.setEnabled(enabled);
		this.passField.setEnabled(enabled);
		this.selfRegCheck.setEnabled(enabled);
		this.connectButton.setEnabled(enabled);
	}
}